package com.xuecheng.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.system.model.dto.CourseCategoryDTO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.system.convert.CourseCategoryConvert;
import com.xuecheng.system.entity.CourseCategory;
import com.xuecheng.system.entity.ex.CourseCategoryNode;
import com.xuecheng.system.mapper.CourseCategoryMapper;
import com.xuecheng.system.service.CourseCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;


@Service
public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper, CourseCategory> implements CourseCategoryService {

    /**
     * 查询树形结构数据
     * @return
     */
    @Override
    public List<CourseCategoryDTO> queryThreeNodes() {


        //mp 调用mapper
        CourseCategoryMapper baseMapper = getBaseMapper();

        //调用查询方法
        List<CourseCategoryNode> courseCategoryNodes = baseMapper.queryThreeNodes();

        //判断是否为空
        if(null == courseCategoryNodes){
            return Collections.emptyList();
        }

        //将po转为dto并封装返回

        return CourseCategoryConvert.INSTANCE.entitys2dtos(courseCategoryNodes);
    }

    //根据id查询课程分类信息
    public RestResponse<CourseCategoryDTO> queryCourseCategoryById(String courseCategoryId) {

        //判断关键数据
        if(StringUtil.isBlank(courseCategoryId)){
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }

        //查询课程分类数据
        CourseCategory courseCategory = this.getById(courseCategoryId);

        //判读数据是否存在
        if(ObjectUtils.isEmpty(courseCategory)){
            ExceptionCast.cast(CommonErrorCode.E_100104);
        }

        //转为dto
        CourseCategoryDTO courseCategoryDTO = CourseCategoryConvert.INSTANCE.po2dto(courseCategory);

        //返回数据
        return RestResponse.success(courseCategoryDTO);
    }
}
